home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / examples / exam14 / class1.d < prev    next >
Text File  |  1995-09-27  |  5KB  |  155 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  *
  7.  *    This source code is CONFIDENTIAL and
  8.  *    PROPRIETARY to Algorithms Corporation. Unauthorized
  9.  *    distribution, adaptation or use    may
  10.  *    be subject to civil and    criminal penalties.
  11.  *
  12.  *    Copyright (c) 1993 Algorithms Corporation
  13.  *    3020 Liberty Hills Drive
  14.  *    Franklin, TN  37064
  15.  *
  16.  *    ALL RIGHTS RESERVED.
  17.  *
  18.  *
  19.  *
  20.  */
  21.  
  22.  
  23.  /*  This example adds a class variable to the previous examples.
  24.      Class variables are shared by all instances of a class, that is,
  25.      there's only copy and it is associated to the class as a whole.
  26.  
  27.      Notice the class: declaration.  After this declaration everything
  28.      that follows is a class variable.  There is also a similar declaration
  29.      used for instance variables called instance:   However, this declaration
  30.      is rarely needed because it is the default state when defining a new
  31.      class.
  32.  
  33.      Note the use of the standard nameing convension of nameing class
  34.      variables with a leading 'c' (for class) followed by an upper case
  35.      letter.  Again, this is just a convention which is not required by
  36.      Dynace.
  37.  
  38.      Like the instance variable section, any variable whose type is not
  39.      explicitly declared will default to type 'object'.
  40. */
  41.  
  42. defclass  Class1  {
  43.     char    iName[30];
  44.     int    iCode;
  45.     iData;
  46.   class:
  47.       int    cNumInstances;
  48. };
  49.  
  50. /*  Define the New class method.  Since it is a class method it must be
  51.     introduced with 'cmeth' or 'cvmeth'.
  52.  
  53.     Since no return type is declared 'object' is assumed.
  54.  
  55.     Since no arguments are declared, this method will only have the minimum,
  56.     and default argument 'object self'.  Note, however, that in the case of
  57.     class methods, 'self' will always refer to the class object as apposed
  58.     to an instance object as with instance methods.
  59.  
  60.     The first line increments the cNumInstances class variable in order
  61.     to note the new instance being created.  Notice how the class variables
  62.     are directly accessable within the method.  This is also true within
  63.     instance methods.
  64.  
  65.     The second line calls the superclass of this class (in this case 'Object')
  66.     with the generic 'gNew' and a sole argument 'self' in order to create
  67.     in instance of the current class (pointed to by self).  This is done
  68.     because only the New method associated to the Object class actually
  69.     knows how to allocate a new instance object.  Once the new instance object
  70.     is allocated (with the shown super call) it may be further initialized.
  71.  
  72.     In this case the new instance object is simply returned.
  73. */
  74.  
  75. cmeth    gNew()
  76. {
  77.     cNumInstances++;
  78.     return gNew(super);
  79. }
  80.  
  81. /*  This next class method simply returns the value of the cNumInstances
  82.     class variable.  Like all methods its first argument is assumed to be
  83.     'object self'.  And since this is a class method self will always refer
  84.     to the class object.  */
  85.  
  86.  
  87. cmeth    int    gNumInstances()
  88. {
  89.     return cNumInstances;
  90. }
  91.  
  92. /*  This next instance method is used to deallocate or free an instance
  93.     of this class.  It is being associated to two generics (gDeepDispose
  94.     and gDispose).
  95.  
  96.     The first thing it does is decrement cNumInstances in order to keep
  97.     that variable in sync since this method will free the instance passed
  98.     (as the first argument).
  99.  
  100.     The second line calls the gDispose generic with the single argument,
  101.     self on the superclass of this class (which happens to be Object).
  102.     This is done because the Dispose method associated with the Object class
  103.     is the only routine which has the ability to dispose of a Dynace object.
  104.  
  105.     The final line return a NULL.  This is a convention which allows the
  106.     code which called the gDispose generic to NULL out the object pointer
  107.     with the same expression.
  108.  
  109.     Note that prior to the definition of this method (as in the previous
  110.     examples), when the user evoked the gDispose generic on an instance of
  111.     this class, since no Dispose method was defined, the system automatically
  112.     executed the Dispose method associated with the Object class because
  113.     the Object class is the superclass of this class and Dynace automatically
  114.     performs this search up the inheratince hierarchy until a matching method
  115.     is found.  This is also true of the New class method defined above.
  116. */
  117.  
  118. imeth    object    gDeepDispose, gDispose ()
  119. {
  120.     cNumInstances--;
  121.     gDispose(super self);
  122.     return NULL;
  123. }
  124.  
  125. imeth    gSetName(char *name)
  126. {
  127.     strcpy(iName, name);
  128.     return self;
  129. }
  130.  
  131. imeth    char    * gGetName : get_name ()
  132. {
  133.     return iName;
  134. }
  135.  
  136.  
  137. /*
  138.  *
  139.  *    This source code is CONFIDENTIAL and
  140.  *    PROPRIETARY to Algorithms Corporation. Unauthorized
  141.  *    distribution, adaptation or use    may
  142.  *    be subject to civil and    criminal penalties.
  143.  *
  144.  *    Copyright (c) 1993 Algorithms Corporation
  145.  *    3020 Liberty Hills Drive
  146.  *    Franklin, TN  37064
  147.  *
  148.  *    ALL RIGHTS RESERVED.
  149.  *
  150.  *
  151.  *
  152.  */
  153.  
  154.  
  155.